using System;
using System.Collections.Generic;
using System.Text;
using PicoInterface;

namespace RapidHDL
{
    public static class FPGATest
    {
        public static string IOTestMessage(string psImage, int piAddress, int piRepeat)
        {
            Timer oTimer = new Timer();
         
            PicoIO.Attach(psImage);
            PicoInvoke.WriteDevice(piAddress, 7);
                   
            oTimer.Start();
            for (int iRepeat = 0; iRepeat < piRepeat; iRepeat++)
            {
                for (int iCount = 0; iCount < 65536; iCount++)
                {
                    int iResult = PicoInvoke.ReadDevice(piAddress);
                    if (iResult != 7)
                    {
                        oTimer.Stop();
                        return "IO Test Failed after " + oTimer.Duration.ToString() + "." + "  " + iResult.ToString();
                        //return "IO Test Failed on " + iRepeat.ToString() + " * " + iResult.ToString() + " after " + oTimer.Duration.ToString() + " & " + IOPerSec(iRepeat,iResult,oTimer.Duration) + " per sec";;
                    }
                }
            }

            oTimer.Stop();
            return "IO Test Complete - " + oTimer.Duration.ToString() + " & " + IOPerSec(piRepeat,0,oTimer.Duration) + " per sec";
        }

        public static double IOPerSec(int piRepeat, int piCount, double piTimer)
        {
            return (65536 * piRepeat + piCount) / piTimer;
        }
    }
}
